import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import ModbusDemo 1.0

Page {
    id: serverPage
    padding: 10

    ColumnLayout {
        anchors.fill: parent
        spacing: 20

        // 状态和控制区域
        GroupBox {
            title: qsTr("Server Control")
            Layout.fillWidth: true

            ColumnLayout {
                anchors.fill: parent

                RowLayout {
                    Layout.fillWidth: true

                    Label {
                        text: qsTr("Status:")
                        Layout.preferredWidth: 80
                    }

                    Label {
                        text: modbusServer.status
                        color: modbusServer.running ? "green" : "red"
                        Layout.fillWidth: true
                    }
                }

                RowLayout {
                    Layout.fillWidth: true
                    spacing: 10

                    SpinBox {
                        id: portSpinBox
                        from: 1
                        to: 65535
                        value: 502
                        Layout.preferredWidth: 120
                    }

                    Button {
                        text: modbusServer.running ? qsTr("Stop Server") : qsTr("Start Server")
                        onClicked: {
                            if (!modbusServer.running) {
                                modbusServer.startServer(portSpinBox.value)
                            } else {
                                modbusServer.stopServer()
                            }
                        }
                    }
                }
            }
        }

        // 数据显示区域
        GroupBox {
            title: qsTr("Current Data")
            Layout.fillWidth: true
            Layout.fillHeight: true

            ScrollView {
                anchors.fill: parent
                clip: true

                ColumnLayout {
                    width: parent.width
                    spacing: 10

                    Label {
                        text: qsTr("Server is ") + (modbusServer.running ? qsTr("running") : qsTr("stopped")) + 
                              qsTr(" on port ") + portSpinBox.value
                        font.bold: true
                    }

                    Label {
                        text: qsTr("The following data is available via Modbus TCP:")
                        wrapMode: Text.WordWrap
                        Layout.fillWidth: true
                    }

                    GridLayout {
                        columns: 3
                        Layout.fillWidth: true
                        columnSpacing: 20
                        rowSpacing: 10

                        Label { text: qsTr("Register 0:") }
                        Label { text: qsTr("ID (1 register)") }
                        Label { text: modbusServer.deviceId.toString() }

                        Label { text: qsTr("Register 1-10:") }
                        Label { text: qsTr("Name (10 registers)") }
                        Label { text: modbusServer.deviceName }

                        Label { text: qsTr("Register 11-12:") }
                        Label { text: qsTr("Value (2 registers)") }
                        Label { text: modbusServer.deviceValue.toFixed(2) }

                        Label { text: qsTr("Register 13:") }
                        Label { text: qsTr("Status (1 register)") }
                        Label { text: modbusServer.deviceStatus ? "On" : "Off" }

                        Label { text: qsTr("Register 14-15:") }
                        Label { text: qsTr("Timestamp (2 registers)") }
                        Label { 
                            text: new Date(modbusServer.deviceTimestamp * 1000).toLocaleString()
                        }

                        Label { text: qsTr("Register 16-17:") }
                        Label { text: qsTr("Temperature (2 registers)") }
                        Label { text: modbusServer.temperature.toFixed(1) + "°C" }

                        Label { text: qsTr("Register 18-19:") }
                        Label { text: qsTr("Humidity (2 registers)") }
                        Label { text: modbusServer.humidity.toFixed(1) + "%" }

                        Label { text: qsTr("Register 20:") }
                        Label { text: qsTr("Pressure (1 register)") }
                        Label { text: modbusServer.pressure.toString() + " hPa" }

                        Label { text: qsTr("Register 21:") }
                        Label { text: qsTr("Alarm Status (1 register)") }
                        Label { text: modbusServer.alarmStatus.toString() }

                        Label { text: qsTr("Register 22-36:") }
                        Label { text: qsTr("Description (15 registers)") }
                        Label { text: modbusServer.description }
                    }
                }
            }
        }
    }

    // 错误提示对话框
    Popup {
        id: errorPopup
        anchors.centerIn: parent
        width: 400
        modal: true
        focus: true
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside

        contentItem: ColumnLayout {
            spacing: 20

            Label {
                id: errorLabel
                text: ""
                wrapMode: Text.WordWrap
                Layout.fillWidth: true
            }

            Button {
                text: qsTr("Close")
                onClicked: errorPopup.close()
                Layout.alignment: Qt.AlignRight
            }
        }
    }

    // 错误处理
    Connections {
        target: modbusServer
        function onErrorOccurred(error) {
            errorLabel.text = error
            errorPopup.open()
        }
    }
}
